package cn.tedu.weibo.controller;

import cn.tedu.weibo.common.response.ResultVO;
import cn.tedu.weibo.common.response.StatusCode;
import cn.tedu.weibo.mapper.WeiboMapper;
import cn.tedu.weibo.pojo.dto.WeiboDTO;
import cn.tedu.weibo.pojo.entity.Weibo;
import cn.tedu.weibo.pojo.vo.UserVO;
import cn.tedu.weibo.pojo.vo.WeiboDetailVO;
import cn.tedu.weibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

//@Controller
//@ResponseBody
@RestController
@RequestMapping("/v1/weibo/")
@Api(tags = "02.微博模块")
@Slf4j
@Validated
public class WeiboController {
    // 自动装配
    @Autowired(required = false)
    private WeiboMapper weiboMapper;


    /**发布微博功能
     * http://localhost:8080/v1/weibo/insert
     *
     * @RequestBody 描述方法参数时，表示要将客户端传递过来的json格式字符串转换为参数类型的对象
     * */
    @PostMapping("insert")
    @ApiOperation(value = "发布微博功能")
    public int insert(@Validated @RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return 2; // 未登录
            //return StatusCode.NOT_LOGIN.getCode();
        }

        // 发微博：实体类Weibo
        // 复制属性：BeanUtils.copyProperties(xxxDTO, xxx);
        // 定义接口方法
        // xml配置
        // 重启工程浏览器测试
        // 数据库中测试：SELECT * FROM weibo;
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO, weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        // 存数据表
        weiboMapper.insert(weibo);

        return 1;
    }

    /**首页微博列表*/
    @GetMapping("selectIndex")
    @ApiOperation(value = "首页微博列表功能")
    public List<WeiboIndexVO> selectIndex(){

        return weiboMapper.selectIndex();
    }

    /**微博详情列表
     * http://localhost:8080/v1/weibo/selectById?id=1
     */
    @GetMapping("selectById")
    @ApiOperation(value = "微博详情功能")
    @ApiImplicitParam(name="id",value = "微博id",required = true,example = "1",dataType = "int")
    public WeiboDetailVO selectDetail(int id){
        //System.out.println("id="+id);
        log.debug("id's value is {}",id);
        if(id==-1)
            throw new IllegalArgumentException("id值无效");
        // 无需校验用户登录状态
        return weiboMapper.selectDetail(id);
    }
 //Controller级别的异常处理方法，只能处理当前Controller类内部出现的异常（不推荐）
//    @ExceptionHandler(RuntimeException.class)
//    public ResultVO doHandleRuntimeException(RuntimeException ex){
//        return new ResultVO(5000,ex.getMessage(),null);
//    }
   // ==================================================

    /**首页微博列表*/
    @GetMapping("doSelectIndex")
    @ApiOperation(value = "微博列表的统一封装")
    @ResponseBody
    public ResultVO doSelectIndex(){
        return new ResultVO(weiboMapper.selectIndex());
    }
    //我们返回的这个值底层是如何转换为Json格式字符串，底层用到了一个RequestResponseBodyMethodProcessor

    /**微博详情列表
     * http://localhost:8080/v1/weibo/doSelectById/1
     * */
    @GetMapping("doSelectById/{id}")
    @ApiOperation(value = "微博详情功能")
    @ApiImplicitParam(name="id",value = "微博id",required = true,example = "1",dataType = "int")
    public WeiboDetailVO doSelectDetail(@PathVariable("id") int id){
        // 无需校验用户登录状态
        //System.out.println("id="+id);
        return weiboMapper.selectDetail(id);
    }

}

















